import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { Post } from './entities/post.entity';

@Injectable()
export class PostsService {
  constructor(
    @InjectRepository(Post)
    private readonly postRepository: Repository<Post>,
  ) {}
 async create(createPostDto: CreatePostDto):Promise<Global.RuleResType<Post>> {
    const { username, password, email, phone, avatar } = createPostDto;
    const data = await this.postRepository.save({
      username,
      password,
      email,
      phone,
      avatar,
    });
    return { code: 200, message: '创建成功', data };

  }

 async findAll() {
    const data = await this.postRepository.find()
    return { code: 200, message: '查询成功', data };
    // return `This action returns all posts`;
  }

  async findOne(id: number) {
    const data = await this.postRepository.find({where:{id:id}})
    return { code: 200, message: '查询成功', data };
    // return `This action returns a #${id} post`;
  }

  async update(id: number, updatePostDto: UpdatePostDto) {
    // return `This action updates a #${id} post`;
    const data = await this.postRepository.update(id,updatePostDto)
    return { code: 200, message: '更新成功', data };
  }

 async remove(id: number) {
    const data = await this.postRepository.delete(id)
    return { code: 200, message: '删除成功', data };
  }
}
